/**
 * Centro de Informática - UFPE
 * Disc.: Plataformas de Distribuição (IN1118)
 * Exercício 02 - Implementação da camada de interoperabilidade para o uORB.
 * Alunos: Fábio Chicout (fcfmc) / Alysson Barros (abs4)
 */
package br.ufpe.cin.if1118.exerc02bib.interop;

import br.ufpe.cin.if1118.exerc02bib.transport.Buffer;

/**
 *
 * @author Fábio Chicout
 */
public class PDU {

    private Buffer buffer;

    public void send() {
        buffer = new Buffer();
        
//        // Magic!
//        addChar('G');
//        addChar('I');
//        addChar('O');
//        addChar('P');
//
//        // Version
//        addInt(1);
//        addInt(0);
//
//        // It's big endian
//        addInt(0);

    // Body Lenght
    // addLong();
    }

    public long getLong(int index) {
        long result = 0;
        result = result | (0xFF00000000000000L & (((long) buffer.getByteData().get(index + 0)) << 56));
        result = result | (0x00FF000000000000L & (((long) buffer.getByteData().get(index + 1)) << 48));
        result = result | (0x0000FF0000000000L & (((long) buffer.getByteData().get(index + 2)) << 40));
        result = result | (0x000000FF00000000L & (((long) buffer.getByteData().get(index + 3)) << 32));
        result = result | (0x00000000FF000000L & (((long) buffer.getByteData().get(index + 4)) << 24));
        result = result | (0x0000000000FF0000L & (((long) buffer.getByteData().get(index + 5)) << 16));
        result = result | (0x000000000000FF00L & (((long) buffer.getByteData().get(index + 6)) << 8));
        result = result | (0x00000000000000FFL & (((long) buffer.getByteData().get(index + 7))));
        return result;
    }

    public int getInt(int index) {
        int result = 0;
        result = result | (0xFF000000 & (buffer.getByteData().get(index + 0) << 24));
        result = result | (0x00FF0000 & (buffer.getByteData().get(index + 1) << 16));
        result = result | (0x0000FF00 & (buffer.getByteData().get(index + 2) << 8));
        result = result | (0x000000FF & (buffer.getByteData().get(index + 3)));
        return result;
    }

    public short getShort(int index) {
        int result = 0;
        result = result | (0x0000FF00 & (buffer.getByteData().get(index + 0) << 8));
        result = result | (0x000000FF & (buffer.getByteData().get(index + 1)));
        return ((short) result);
    }

    public char getChar(int index) {
        char result = (char) (buffer.getByteData().get(index) & 0xFF);
        return result;
    }

    public boolean getBoolean(int index) {
        boolean result;
        if (buffer.getByteData().get(index) == (byte) 1) {
            result = true;
        } else {
            result = false;
        }

        return result;
    }

    public String getString(int index, int lenght) {
        byte[] byteString = new byte[lenght];
        String result;

        for (int i = 0; i < byteString.length; i++) {
            byteString[i] = buffer.getByteData().get(index + i);
        }
        result = new String(byteString, 0, lenght);
        return result;
    }

    public void addLong(long value) {
        byte[] result = new byte[8];
        for (int i = 0; i < result.length; i++) {
            result[7 - i] = (byte) (value & 0xFF);
            value = value >> 8;
        }
        buffer.add(result);
    }

    public void addInt(int value) {
        byte[] result = new byte[4];
        for (int i = 0; i < result.length; i++) {
            result[3 - i] = (byte) (value & 0xFF);
            value = value >> 8;
        }
        System.out.println("Is null: " + (result == null));
        System.out.println("byte values:" + result[0] + result[1] + result[2] + result[3]);
        buffer.add(result);
    }

    public void addShort(short value) {
        byte[] result = new byte[2];
        for (int i = 0; i < result.length; i++) {
            result[1 - i] = (byte) (value & 0xFF);
            value = (short) (value >> 8);
        }
        buffer.add(result);
    }

    public void addChar(char value) {
        byte result = (byte) (value & 0xFF);
        buffer.add(result);
    }

    public void addBoolean(boolean value) {
        if (value) {
            buffer.add(new Byte((byte) 1));
        } else {
            buffer.add(new Byte((byte) 0));
        }
    }

    public void addString(String value) {
        buffer.add(value.getBytes());
    }

    public static void main(String[] args) {
        PDU instance = new PDU();
        instance.send();
    }
}
